package com.bo.db;

import io.swagger.annotations.ApiModel;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Accessors(chain = true)
@ApiModel(value="配比对象接受保持", description="配比对象接受保持")
@Component
public class DataSourceMDBConfig {


    /**
     * 获取 Access 数据库连接
     *
     * @param path 数据库文件路径
     * @param user 用户名
     * @param pwd  密码
     * @return 数据库连接
     */
    public Connection getAccessConnection(String path, String user, String pwd) {
        try {
            // 显式加载驱动
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

            return DriverManager.getConnection("jdbc:ucanaccess://" + path, user, pwd);
        } catch (Exception e) {
            throw new RuntimeException("Failed to establish database connection for path: " + path, e);
        }
    }

    /**
     * 关闭数据库连接
     *
     * @param connection 数据库连接
     */
    public void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException("Failed to close database connection: " + e.getMessage(), e);
            }
        }
    }


}
